using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.Logic
{
    public class FormulaFunction_Sqrt : FormulaFunction
    {
        public override EFormulaFunctionType funcType => EFormulaFunctionType.Sqrt;

        public override void Calculate(Stack<IFormulaOperand> rCalopd, IFormulaObject rSelf, IFormulaObject rTarget, Formula rFromFormula)
        {
            IFormulaOperand opd1 = rCalopd.Pop();
            if (opd1.TryGet<LFloat>(out var op1, rSelf, rTarget, rFromFormula))
            {
                IFormulaOperand result = FormulaHelper.FetchOperand(EFormulaOperandType.Num);
                result.Set(LMath.Sqrt(op1));
                rCalopd.Push(result);
            }
            else
            {
                throw new Exception("Sqrt func need one number");
            }

            opd1.TryFree();
        }
    }
}